package com.btpj.shop.controller;

import com.btpj.shop.dto.CommonPage;
import com.btpj.shop.dto.CommonResult;
import com.btpj.shop.dto.IndexConfigDto;
import com.btpj.shop.dto.param.IndexConfigParam;
import com.btpj.shop.service.IndexConfigService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "首页配置项相关接口")
@RequestMapping("/indexConfig")
@RestController
public class IndexConfigController {

    @Resource
    private IndexConfigService indexConfigService;

    @ApiOperation(value = "添加首页配置项", notes = "传递type(1-搜索框热搜 2-搜索下拉框热搜 3-(首页)热销商品 4-(首页)新品上线 5-(首页)最新推荐)")
    @PostMapping("/admin/add")
    public CommonResult<Void> addIndexConfig(@RequestBody @Validated IndexConfigParam indexConfigParam) {
        if (indexConfigService.addIndexConfig(indexConfigParam)) {
            return CommonResult.success("添加首页配置项成功");
        }
        return CommonResult.error("服务器出错，添加首页配置项失败");
    }

    @ApiOperation(value = "修改首页配置项", notes = "传递type(1-搜索框热搜 2-搜索下拉框热搜 3-(首页)热销商品 4-(首页)新品上线 5-(首页)最新推荐)")
    @PutMapping("/admin/update/{id}")
    public CommonResult<Void> addIndexConfig(@PathVariable Long id, @RequestBody @Validated IndexConfigParam indexConfigParam) {
        if (!indexConfigService.exists(id)) {
            return CommonResult.error("不存在指定ID为" + id + "的首页配置项");
        }

        if (indexConfigService.updateIndexConfig(id, indexConfigParam)) {
            return CommonResult.success("修改首页配置项成功");
        }
        return CommonResult.error("服务器出错，修改首页配置项失败");
    }

    @ApiOperation(value = "获取首页配置项列表", notes = "不传递pageNo或pageSize返回所有")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1-搜索框热搜 2-搜索下拉框热搜 3-(首页)热销商品 4-(首页)新品上线 5-(首页)最新推荐", required = true, paramType = "param", dataType = "Byte"),
            @ApiImplicitParam(name = "pageNo", value = "第几页", paramType = "param", dataType = "Int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", paramType = "param", dataType = "Int"),
    })
    @GetMapping("/pageList")
    public CommonResult<CommonPage<IndexConfigDto>> getIndexConfigs(@RequestParam @Validated @NonNull Byte type,
                                                                    @RequestParam Integer pageNo,
                                                                    @RequestParam Integer pageSize) {
        if (pageNo != null && pageSize != null) {
            PageHelper.startPage(pageNo, pageSize);
        }
        List<IndexConfigDto> list = indexConfigService.getIndexConfigsByType(type);
        return CommonResult.success(CommonPage.listPage(list));
    }

    @ApiOperation(value = "批量删除首页配置项")
    @ApiImplicitParam(name = "ids", value = "首页配置项ID集", required = true, allowMultiple = true, paramType = "path", dataType = "Long")
    @DeleteMapping("/admin/delete/{ids}")
    public CommonResult<Void> getIndexConfigs(@PathVariable Long[] ids) {
        for (Long id : ids) {
            if (!indexConfigService.exists(id)) {
                return CommonResult.error("不存在指定ID为" + id + "的首页配置项");
            }
        }

        if (indexConfigService.deleteByIds(ids)) {
            return CommonResult.success("删除首页配置项成功");
        }
        return CommonResult.error("服务器出错,删除首页配置项失败");
    }
}
